package com.mbc.rmt.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.data.OfficeCOADataList;
import com.mbc.rmt.entities.OfficeCOA;
import com.mbc.rmt.entities.OfficeCOARpt;

public class OfficeCOADAOImpls extends DAO<OfficeCOA> implements OfficeCOADAO{

	public OfficeCOADAOImpls(Connection conn){
		this.conn = conn;
		this.table_name = "OfficeCOA";
	}
	public void create(OfficeCOA  entity)throws SQLException{
		query = "insert into "+table_name+" ( office_coa_id,office_coa_code,description,accpayable,unrealize_ex_gl,write_off,accpayable_dept,unrealize_ex_gl_dept,write_off_dept,BranchId) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i=1;
		pstmt.setLong(i++, entity.getOffice_coa_id());
		pstmt.setString(i++, entity.getOffice_coa_code());
		pstmt.setString(i++, entity.getDescription());
		pstmt.setLong(i++, entity.getAccpayable());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl());
		pstmt.setLong(i++, entity.getWrite_off());
		pstmt.setLong(i++, entity.getAccpayable_dept());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl_dept());
		pstmt.setLong(i++, entity.getWrite_off_dept());
		pstmt.setLong(i++, entity.getBranchID());
		pstmt.executeUpdate();
		pstmt.close();
		
	}
	public void update(OfficeCOA entity)throws SQLException{
		query = "update "+table_name+" set office_coa_code = ?," +
				"description = ?, accpayable = ?, unrealize_ex_gl = ?," +
				"write_off = ?, accpayable_dept = ?, unrealize_ex_gl_dept = ?, " +
				"write_off_dept = ?, BranchId = ? where office_coa_id = "+entity.getOffice_coa_id();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setString(i++, entity.getOffice_coa_code());
		pstmt.setString(i++, entity.getDescription());
		pstmt.setLong(i++, entity.getAccpayable());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl());
		pstmt.setLong(i++, entity.getWrite_off());
		pstmt.setLong(i++, entity.getAccpayable_dept());
		pstmt.setLong(i++, entity.getUnrealize_ex_gl_dept());
		pstmt.setLong(i++, entity.getWrite_off_dept());
		pstmt.setLong(i++, entity.getBranchID());
		pstmt.executeUpdate();
		pstmt.close();
	}
	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where office_coa_id = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
		
	}
	@Override
	protected OfficeCOA readRecord(ResultSet rs) throws SQLException {
		OfficeCOA l_entity = new OfficeCOA();
		l_entity.setOffice_coa_id(rs.getLong("office_coa_id"));
		l_entity.setOffice_coa_code(rs.getString("office_coa_code"));
		l_entity.setDescription(rs.getString("description"));
		
		l_entity.setAccpayable(rs.getLong("accpayable"));
		l_entity.setAccpayable_code(rs.getString("accpayable_code"));

		l_entity.setUnrealize_ex_gl(rs.getLong("unrealize_ex_gl"));
		l_entity.setUnrealize_ex_gl_code(rs.getString("unrealize_ex_gl_code"));

		l_entity.setWrite_off(rs.getLong("write_off"));
		l_entity.setWrite_off_code(rs.getString("write_off_code"));

		l_entity.setAccpayable_dept(rs.getLong("accpayable_dept"));
		l_entity.setAccpayable_dept_code(rs.getString("accpayable_dept_code"));

		l_entity.setUnrealize_ex_gl_dept(rs.getLong("unrealize_ex_gl_dept"));
		l_entity.setUnrealize_ex_gl_dept_code(rs
				.getString("unrealize_ex_gl_dept_code"));

		l_entity.setWrite_off_dept(rs.getLong("write_off_dept"));
		l_entity.setWrite_off_dept_code(rs.getString("write_off_dept_code"));
		return l_entity;
	}
	
	@Override
	public OfficeCOADataList findOfficeCOA(OfficeCOA cri)
			throws SQLException {
		OfficeCOADataList l_DataList = new OfficeCOADataList();
		ArrayList<OfficeCOA> l_entityList = new ArrayList<OfficeCOA>();
		/*String l_cri = " and (office_coa_code like N'%" +cri.getKeyword() +"%'"+
						" or acoa.description like N'%" + cri.getKeyword() + "%')"+
						" and acoa.BranchId = " + cri.getBranchId();*/
		String l_cri = " and ( " + cri.getFieldname() + " like N'%" + cri.getKeyword() + "%') and acoa.BranchId = " + cri.getBranchId();
		
		query = "set rowcount "
				+ cri.getSize()
				+ " select * from (select row_number() over(order by office_coa_id desc)as [rowno], "
				+ "acoa.office_coa_id,acoa.office_coa_code,acoa.description, acoa.BranchId, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_code, acoa.accpayable_dept,acrdep.departmentCode accpayable_dept_code, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_code,acoa.unrealize_ex_gl_dept,urdep.departmentCode unrealize_ex_gl_dept_code, "
				+ "acoa.write_off,wcoa.AC_ID write_off_code,acoa.write_off_dept,wdep.departmentCode write_off_dept_code " 
				+ "from dbo.OfficeCOA acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.departmentID "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.departmentID "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.departmentID "
				+ " where 1 = 1" + l_cri + ") " + table_name + " where rowno > "
				+ cri.getIndex();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while(rs.next()){
				l_entityList.add(readRecord(rs));
		}
		l_DataList.setEntityList(l_entityList);
		rs.close();
		pstmt.close();
		String l_cri1 = " and (office_coa_code like N'%" +cri.getKeyword() +"%'"+
				" or description like N'%" + cri.getKeyword() + "%')" +
				" and BranchId = " + cri.getBranchId();
		query = "select count(office_coa_code) from "+table_name+" where 1 = 1"+ l_cri1;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next()){
			l_DataList.setTotalRecord(rs.getLong(1));
		}
		rs.close();
		pstmt.close();
		return l_DataList;
	}

	public OfficeCOA findByCode(String code,long branchId) throws SQLException {
		OfficeCOA l_OfficeCOA = null;
		query = " select row_number() over(order by office_coa_id desc)as [rowno], "
				+ "acoa.office_coa_id,acoa.office_coa_code,acoa.description, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_code, acoa.accpayable_dept,acrdep.departmentCode accpayable_dept_code, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_code,acoa.unrealize_ex_gl_dept,urdep.departmentCode unrealize_ex_gl_dept_code, "
				+ "acoa.write_off,wcoa.AC_ID write_off_code,acoa.write_off_dept,wdep.departmentCode write_off_dept_code "
				+ "from dbo.OfficeCOA acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.departmentID "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.departmentID "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.departmentID "
				+ " where office_coa_code = '" + code + "' and acoa.BranchId = " + branchId;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_OfficeCOA = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_OfficeCOA;
	}
	protected OfficeCOA readRecord1(ResultSet rs) throws SQLException {
		OfficeCOA l_entity = new OfficeCOA();
		l_entity.setOffice_coa_id(rs.getLong("office_coa_id"));
		l_entity.setOffice_coa_code(rs.getString("office_coa_code"));
		l_entity.setDescription(rs.getString("description"));
		
		l_entity.setAccpayable(rs.getLong("accpayable"));
		l_entity.setUnrealize_ex_gl(rs.getLong("unrealize_ex_gl"));
		l_entity.setWrite_off(rs.getLong("write_off"));
		l_entity.setAccpayable_dept(rs.getLong("accpayable_dept"));
		l_entity.setUnrealize_ex_gl_dept(rs.getLong("unrealize_ex_gl_dept"));
		l_entity.setWrite_off_dept(rs.getLong("write_off_dept"));
		return l_entity;
	}
	public ResultSet prepareOfficeCOAReport(OfficeCOARpt office)
			throws SQLException {
		ResultSet l_rs;
		String l_query = "dbo.sp_cr_OfficeCOARpt ?, ?, ?";

		CallableStatement l_pstmt = conn.prepareCall(l_query);
		OfficeCOARptParameter(l_pstmt, office);
		l_rs = l_pstmt.executeQuery();
		return l_rs;

	}
	private void OfficeCOARptParameter(CallableStatement l_pstmt,
			OfficeCOARpt office) throws SQLException {
		int i = 1;

		l_pstmt.setString(i++, office.getFromOfficeCOAID());
		l_pstmt.setString(i++, office.getToOfficeCOAID());
		l_pstmt.setLong(i++, office.getBranchID());
	}
	public OfficeCOADataList getOfficeCOAList(OfficeCOARpt office) throws SQLException {
		OfficeCOADataList l_DataList = new OfficeCOADataList();
		ArrayList<OfficeCOA> l_OfficeCOAList = new ArrayList<OfficeCOA>();
		String l_query = "dbo.sp_cr_OfficeCOARpt ?, ?, ?";
		CallableStatement l_pstmt = conn.prepareCall(l_query);
		OfficeCOARptParameter(l_pstmt, office);
		ResultSet l_rs = l_pstmt.executeQuery();
		while (l_rs.next()) {
			l_OfficeCOAList.add(readRecordFT(l_rs));
		}
		l_DataList.setEntityList(l_OfficeCOAList);
		l_rs.close();
		l_pstmt.close();
		return l_DataList;
	}
	private OfficeCOA readRecordFT(ResultSet rs) throws SQLException {
		OfficeCOA l_entity = new OfficeCOA();
		l_entity.setOffice_coa_code(rs.getString("office_coa_code"));
		l_entity.setDescription(rs.getString("description"));
		l_entity.setAccpayable_code(rs.getString("accpayable"));
		l_entity.setAccpayable_dept_code(rs.getString("accpayable_dept"));
		l_entity.setUnrealize_ex_gl_code(rs.getString("unrealize_ex_gl_code"));
		l_entity.setUnrealize_ex_gl_dept_code(rs.getString("unrealize_ex_gl_dept_code"));
		l_entity.setWrite_off_code(rs.getString("write_off_code"));
		l_entity.setWrite_off_dept_code(rs.getString("write_off_dept_code"));
		return l_entity;
	}
	@Override
	public OfficeCOA findByID(long officeID) throws SQLException {
		OfficeCOA l_officeCOA = new OfficeCOA();
		query = " select "
				+ "acoa.office_coa_id,acoa.office_coa_code,acoa.description, "
				+ "acoa.accpayable,acrcoa.AC_ID accpayable_code, acoa.accpayable_dept,acrdep.departmentCode accpayable_dept_code, "
				+ "acoa.unrealize_ex_gl,urcoa.AC_ID unrealize_ex_gl_code,acoa.unrealize_ex_gl_dept,urdep.departmentCode unrealize_ex_gl_dept_code, "
				+ "acoa.write_off,wcoa.AC_ID write_off_code,acoa.write_off_dept,wdep.departmentCode write_off_dept_code "
				+ "from "+table_name+" acoa "
				+ "LEFT JOIN dbo.COA acrcoa ON acoa.accpayable=acrcoa.COAKey "
				+ "LEFT JOIN dbo.Department acrdep ON acoa.accpayable_dept=acrdep.departmentID "
				+ "LEFT JOIN dbo.COA urcoa ON acoa.unrealize_ex_gl=urcoa.COAKey "
				+ "LEFT JOIN dbo.Department urdep ON acoa.unrealize_ex_gl_dept=urdep.departmentID "
				+ "LEFT JOIN dbo.COA wcoa ON acoa.write_off=wcoa.COAKey "
				+ "LEFT JOIN dbo.Department wdep ON acoa.write_off_dept=wdep.departmentID "
				+ " where office_coa_id = " + officeID;
		
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_officeCOA = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_officeCOA;
	}
}
